/**
 * 
 */
package org.youisoft.summer.evaluator.function.service;

import org.youisoft.summer.evaluator.InvokeException;
import org.youisoft.summer.evaluator.context.CalContext;
import org.youisoft.summer.evaluator.function.AbstractFunction;
import org.youisoft.summer.evaluator.function.Function;
import org.youisoft.summer.evaluator.operator.Operator;
import org.youisoft.summer.evaluator.operator.base.ClosureOperator;
import org.youisoft.summer.evaluator.unit.Brace;

/**
 * @author zijiang.jl
 *
 */
public class DeployFunction extends AbstractFunction{
	
	@Override
	protected boolean checkOperator(int index, Operator<?> operator) {
		if(index==0&&ClosureOperator.class.isAssignableFrom(operator.getClass())) return true;
		return false;
	}

	@Override
	protected int minOperatorCount() {
		return 1;
	}

	@Override
	public <C, R, P> Operator<?> doBraceInvoke(final CalContext<C, R, P> context,
			Operator<?>... operator) throws InvokeException {
		final ClosureOperator co=ClosureOperator.class.cast(operator[0]);
		final Function fun=this.findFunction(Brace.class.getName());
		new Thread("deploy thread:"){

			@Override
			public void run() {
				while(!context.isNeedInterrupt()){
					try {
						fun.invoke(context, co);
					} catch (java.lang.Throwable e) {
						logger.error(e);
					}
				}
			}
			
		}.start();

		return null;
	}

	
}
